package com.tomehay.termprojectmanage_server.controller;

import com.tomehay.termprojectmanage_server.pojo.Model.Plan;
import com.tomehay.termprojectmanage_server.pojo.VO.ResultVO;
import com.tomehay.termprojectmanage_server.service.PlanService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Plan)控制层
 *
 * @author makejava
 * @since 2023-06-24 20:04:22
 */
@RestController
@RequestMapping("/api/plan")
public class PlanController {
    /**
     * 服务对象
     */
    @Resource
    private PlanService planService;

    /**
     * 通过主键查询单条数据
     *
     * @param plan 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.GET)
    public ResultVO<Plan> selectOne(Plan plan) {
        Plan result = planService.selectById(plan.getId());
        if(result != null){
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 新增一条数据
     *
     * @param plan 实体类
     * @return Response对象
     */
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    public ResultVO<Plan> insert(@RequestBody Plan plan) {
        int result = planService.insert(plan);
        if (result > 0) {
            return ResultVO.createSuccessResponse(plan);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 修改一条数据
     *
     * @param plan 实体类
     * @return Response对象
     */
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public ResultVO<Plan> update(@RequestBody Plan plan) {
        Plan result = planService.update(plan);
        if (result != null) {
            return ResultVO.createSuccessResponse(result);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 删除一条数据
     *
     * @param plan 参数对象
     * @return Response对象
     */
    @RequestMapping(value = "delete", method = RequestMethod.DELETE)
    public ResultVO<Plan> delete(Plan plan) {
        int result = planService.deleteById(plan.getId());
        if (result > 0) {
            return ResultVO.createSuccessResponse(null);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 查询全部
     *
     * @return Response对象
     */
    @RequestMapping(value = "selectAll", method = RequestMethod.GET)
    public ResultVO<List<Plan>> selectAll() {
        List<Plan> plans = planService.selectAll();
        if (plans != null) {
            return ResultVO.createSuccessResponse(plans);
        }
        return ResultVO.createErrorResponse();
    }

    /**
     * 分页查询
     *
     * @param start 偏移
     * @param limit 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.GET)
    public ResultVO<List<Plan>> selectPage(Integer start, Integer limit) {
        List<Plan> plans = planService.selectPage(start, limit);
        if (plans != null) {
            return ResultVO.createSuccessResponse(plans);
        }
        return ResultVO.createErrorResponse();
    }

    @RequestMapping(value = "selectAllByPid", method = RequestMethod.GET)
    public ResultVO<List<Plan>> selectAllByPid(@RequestParam("pid") Integer pid) {
        List<Plan> plans = planService.selectAllByPid(pid);
        if (plans != null) {
            return ResultVO.createSuccessResponse(plans);
        }
        return ResultVO.createErrorResponse();
    }

    @GetMapping("/changeStatus")
    public ResultVO<Integer> changeStatus(@RequestParam("id") Integer id) {
        planService.changeStatus(id);
        return ResultVO.createSuccessResponse(1);
    }

}

